<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title></title>
</head>
<body>

<!--页面内容开始-->
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <div class="ibox-title">
                    <h5 th:if="${dKey == 'spending'}">[[${application.sysName}]]经费支出审批表</h5>
                    <h5 th:if="${dKey == 'expense'}">[[${application.sysName}]]财务报销单据</h5>
                    <h5 th:if="${dKey == 'repay'}">[[${application.sysName}]]财务报销单据-还款</h5>
                </div>
                <div class="ibox-content">
                    <div class="table-responsive">
                        <div class="m-l-xs toolbar">
                            <button class="btn btn-sm btn-primary" type="button"
                                    onclick="pzcurmainListObj.addOrUpdateHandle()"><i
                                    class="fa fa-plus"></i>&nbsp;新增
                            </button>
                        </div>
                        <table id="pzcurmainList_table" class="table table-striped"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--页面内容结束-->
<!--弹窗：单据作废-->
<div th:replace="web/pz/cur/pzcurmain-invalid::pzcurmainInvalid"></div>
<!--弹窗：单据借款-->
<div th:replace="web/pz/cur/pzcurmain-loan::pzcurmainLoan"></div>
<script type="text/javascript">
    $().ready(function () {
        pzcurmainListObj.initTable();
    })
    var pzcurmainListObj = new Object({
        dataListSelections: [],
        myTable: undefined,
        addOrUpdateHandle: function () {
            centerLoad('web/pz/cur/pzcurmain-detail?dKey=spending', undefined, "新增", true)
        },
        // 单据提交
        saveAudit: function (id) {
            $.mySwalConfirm(`将提交单据，确认吗？`, function () {
                $.myPost({
                    url: `/portal/process/instance/saveAuditProcess.json`,
                    data: {pIds: id},
                    success: function (data) {
                        if (data && data.success) {
                            toastr.success(data.msg, '操作提示')
                            pzcurmainListObj.refreshDataList();
                        } else {
                            toastr.error(data.msg, '操作失败')
                        }
                    }
                })
            });
        },
        // 单据作废
        saveInvalid: function (mainId, isExistApply) {
            pzcurmainInvalidObj.init(mainId, isExistApply);
            $('#pzcurmainInvalid_Modal').modal('show')
        },
        editLoan: function (applyCode, money) {
            pzcurmainLoanObj.init(applyCode, money);
            $('#pzcurmainLoan_Modal').modal('show')
        },
        refreshDataList: function () {
            pzcurmainListObj.myTable.bootstrapTable('refresh');
        },
        initTable: function () {
            pzcurmainListObj.myTable = $('#pzcurmainList_table').bootstrapTable({
                toolbar: ".toolbar",
                pagination: true,
                sidePagination: 'server',
                url: '/pz/cur/pzmain/list/[[${dKey}]]',
                formatNoMatches: function () {  //没有匹配的结果
                    return '暂无数据';
                },
                queryParams: function (param) {
                    return param;
                },
                columns: [{
                    title: '#',
                    align: 'center',
                    //checkbox: true,
                    width: 50,
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                }, {
                    title: '申请单号',
                    field: 'applyCode',
                    formatter: function (value, row, index) {
                        return `<a href="/portal/process/instance2/viewProcess.html?id=${row.id}" target="_blank" data-toggle="tooltip" data-placement="top" title="${row.reason}">${value}</a>`;
                    }
                }, {
                    title: '标题',
                    field: 'title'
                }, {
                    title: '经办人',
                    field: 'agentUsers'
                }, {
                    title: '申请日期',
                    field: 'applyDate',
                    sortable: true
                }, {
                    title: '金额',
                    field: 'money',
                    formatter: function (value, row, index) {
                        return $.moneyFormat(value);
                    },
                    sortable: true
                }, {
                    title: '经费类型',
                    field: 'spendingType',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "一般";
                        } else if (value == 2) {
                            return "出差";
                        } else if (value == 3) {
                            return "专家";
                        } else if (value == 4) {
                            return "补登";
                        } else if (value == 5) {
                            return "提现";
                        }
                        return "";
                    }
                }, {
                    title: '状态',
                    field: 'state',
                    formatter: function (value, row, index) {
                        let str = "";
                        if (value == 1) {
                            str = `<span class="badge">未提交</span>`
                        } else if (value == 2) {
                            str = `<span class="badge">审批</span>`
                        } else if (value == 3) {
                            str = `<span class="badge">结束</span>`
                        } else if (value == 4) {
                            str = `<span class="badge">作废</span>`
                        } else if (value == 5) {
                            str = `<span class="badge">驳回</span>`
                        }
                        if (row.isExpense) {
                            str += `<span class="badge badge-primary">已报销</span>`
                        }
                        if (row.isExistApply) {
                            str += `<span class="badge badge-warning">关联事前</span>`
                        }
                        if (row.isAppoint) {
                            str += `<span class="badge badge-danger">已预约</span>`
                        }
                        if (row.isExistLoan) {
                            str += `<span class="badge badge-info">有借款</span>`
                        }
                        return str;
                    }
                }, {
                    title: '操作',
                    align: 'right',
                    halign: 'left',
                    width: 160,
                    events: {
                        'click .loan': function (e, value, row, index) {
                            pzcurmainListObj.editLoan(row.applyCode, row.money);
                        },
                        'click .edit': function (e, value, row, index) {console.log(row.id);
                            centerLoad(`web/pz/cur/pzcurmain-detail?dKey=${row.applyType}&mainId=${row.id}`, undefined, "修改", true)
                        },
                        'click .send': function (e, value, row, index) {
                            pzcurmainListObj.saveAudit(row.id);
                        },
                        'click .trash': function (e, value, row, index) {
                            pzcurmainListObj.saveInvalid(row.id, row.isExistApply);
                        },
                        'click .withdraw': function (e, value, row, index) {
                            pzcurmainListObj.saveWithdraw(row.tpmId);
                        },
                        'click .print': function (e, value, row, index) {
                            window.open("/web/pz/his/pzmain/printTicket?mainId=" + row.id);
                        },
                        'click .processImg': function (e, value, row, index) {
                            window.open(`/portal/process/processImg.html?deployKey=${row.applyType}&mainId=${row.id}`);
                        },
                        'click .auditProcess': function (e, value, row, index) {
                            window.open("/portal/process/auditProcess.html?mainId=" + row.id);
                        },
                        'click .authBack': function (e, value, row, index) {
                            pzcurmainListObj.authBack(row.tpmId);
                        }
                    },
                    formatter: function (value, row, index) {
                        var opers = [];
                        if (row.state == 1 || row.state == 5) {
                            opers.push(
                                '<a title="编辑" class="edit btn btn-white btn-bitbucket text-success" data-toggle="tooltip" data-placement="top" href="javascript:void(0)">',
                                '<i class="fa fa-edit"></i>',
                                '</a>'
                            );
                            if (row.spendingType > 0) {
                                opers.push(
                                    '<a title="提交" class="send btn btn-white btn-bitbucket text-success" data-toggle="tooltip" data-placement="top" href="javascript:void(0)">',
                                    '<i class="fa fa-send"></i>',
                                    '</a>'
                                );
                            }
                        }
                        if (row.state == 3) {
                            opers.push(`<a class="print btn btn-white btn-bitbucket text-success" data-toggle="tooltip" data-placement="top" title="打印">
                                    <i class="fa fa-print"></i>
                                </a>`)
                        }
                        let menus = [];
                        if (row.applyType == 'expense' && !row.isAppoint && row.state != 4) {// 借款:非作废单据,不存在借款或存在借款但还未处理
                            if (!row.isExistLoan || (row.isExistLoan && !row.isLoanDeal)) {
                                menus.push(`<li><a class="dropdown-item loan" href="#">借款</a></li>`)
                            }
                        }
                        if (row.canRevoke) {
                            menus.push(`<li><a class="dropdown-item withdraw" href="#">撤回</a></li>`)
                        }
                        if (row.state == 1 || row.state == 3 || row.state == 5) {
                            if (row.authUserName == null || row.authUserName == '') {
                                if (!row.isExistLoan || (row.isExistLoan && !row.isLoanDeal)) {
                                    if (!row.isAppoint) {
                                        menus.push(`<li><a class="dropdown-item trash" href="#">作废</a></li>`)
                                    }
                                }
                            } else {
                                if (row.state == 1 || row.state == 5) {
                                    menus.push(`<li><a class="dropdown-item authBack" href="#">退回委托</a></li>`)
                                }
                            }
                        }
                        if (row.state > 1 && row.state != 4) {
                            if (menus.length > 0) {
                                menus.push('<li class="dropdown-divider"></li>')
                            }
                            menus.push(`<li><a class="dropdown-item processImg" href="#">流程图</a></li>`)
                            menus.push(`<li><a class="dropdown-item auditProcess" href="#">审批过程</a></li>`)
                        }
                        if (menus.length > 0) {
                            opers.push(`
                                <div class="btn-group">
                                <button data-toggle="dropdown" class="btn btn-default btn-sm dropdown-toggle text-success">操作 </button>
                                <ul class="dropdown-menu">
                                    ${menus.join("")}
                                </ul>
                            </div>
                            `)
                        }
                        return opers.join('');
                    }
                }],
                responseHandler: function (res) {
                    return {
                        total: res.page.totalCount,
                        rows: res.page.list
                    };
                },
                onLoadSuccess: function (data) {
                    $('[data-toggle="tooltip"]').tooltip()
                }
            });
        }
    })
</script>
</body>
</html>